<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmAddressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="业务名称" align="center" prop="businessName"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="报警信息0" width="120" align="center" prop="index0"/>
      <el-table-column label="报警信息1" width="120" align="center" prop="index1"/>
      <el-table-column label="报警信息2" width="120" align="center" prop="index2"/>
      <el-table-column label="报警信息3" width="120" align="center" prop="index3"/>
      <el-table-column label="报警信息4" width="120" align="center" prop="index4"/>
      <el-table-column label="报警信息5" width="120" align="center" prop="index5"/>
      <el-table-column label="报警信息6" width="120" align="center" prop="index6"/>
      <el-table-column label="报警信息7" width="120" align="center" prop="index7"/>
      <el-table-column label="报警信息8" width="120" align="center" prop="index8"/>
      <el-table-column label="报警信息9" width="120" align="center" prop="index9"/>
      <el-table-column label="报警信息10" width="120" align="center" prop="index10"/>
      <el-table-column label="报警信息11" width="120" align="center" prop="index11"/>
      <el-table-column label="报警信息12" width="120" align="center" prop="index12"/>
      <el-table-column label="报警信息13" width="120" align="center" prop="index13"/>
      <el-table-column label="报警信息14" width="120" align="center" prop="index14"/>
      <el-table-column label="报警信息15" width="120" align="center" prop="index15"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" fixed="right" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备报警信息配置对话框 -->
    <el-dialog :lock-scroll="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form style="height: 500px;overflow: auto;padding-right: 20px" ref="form" :model="form" :rules="rules"
               label-width="120px">
        <el-form-item label="业务名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入业务名称"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="报警信息0" prop="index0">
          <el-input v-model="form.index0" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息1" prop="index1">
          <el-input v-model="form.index1" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息2" prop="index2">
          <el-input v-model="form.index2" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息3" prop="index3">
          <el-input v-model="form.index3" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息4" prop="index4">
          <el-input v-model="form.index4" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息5" prop="index5">
          <el-input v-model="form.index5" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息6" prop="index6">
          <el-input v-model="form.index6" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息7" prop="index7">
          <el-input v-model="form.index7" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息8" prop="index8">
          <el-input v-model="form.index8" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息9" prop="index9">
          <el-input v-model="form.index9" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息10" prop="index10">
          <el-input v-model="form.index10" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息11" prop="index11">
          <el-input v-model="form.index11" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息12" prop="index12">
          <el-input v-model="form.index12" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息13" prop="index13">
          <el-input v-model="form.index13" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息14" prop="index14">
          <el-input v-model="form.index14" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="报警信息15" prop="index15">
          <el-input v-model="form.index15" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAlarmAddress,
  delAlarmAddress,
  getAlarmAddress,
  listAlarmAddress,
  updateAlarmAddress
} from "@/api/mes/alarm_address";

export default {
  name: "AlarmAddress",
  props: {
    device: {
      type: Object,
      default: null
    }
  },
  watch: {
    device(newVal, olcVal) {
      this.init()
      this.getList()
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备报警信息配置表格数据
      alarmAddressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        businessName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessName: [
          {required: true, message: "业务名称不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "地址不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {

  },
  methods: {
    init() {
      this.queryParams.deviceId = this.device.id
    },
    /** 查询设备报警信息配置列表 */
    getList() {
      this.loading = true;
      listAlarmAddress(this.queryParams).then(response => {
        this.alarmAddressList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: this.device.id,
        businessName: null,
        address: null,
        index0: null,
        index1: null,
        index2: null,
        index3: null,
        index4: null,
        index5: null,
        index6: null,
        index7: null,
        index8: null,
        index9: null,
        index10: null,
        index11: null,
        index12: null,
        index13: null,
        index14: null,
        index15: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备报警信息配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlarmAddress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备报警信息配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlarmAddress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarmAddress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备报警信息配置编号为"' + ids + '"的数据项？').then(function () {
        return delAlarmAddress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
